﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using V5_EShop._Class.BLL;
using V5_Common;
using V5_Model;
using V5_BLL;
using System.IO;

namespace V5_EShop.Admin.GZProduct.Brand {
    public partial class Admin_Brand : System.Web.UI.Page {
        #region 开始
        BLLGZBrand bllGZProduct = new BLLGZBrand();
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                string action = InputHelper.SetString(Request["action"]);
                switch (action) {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    default:
                        this.Bind_DataList();
                        break;
                }
            }
        }

        #region 增删改
        private void Bind_DataList() {
            this.MultiViewList.ActiveViewIndex = 0;
            int RecordCount = 0;
            DataTable dt = bllGZProduct.GetList("*", Pager.PageSize, Pager.CurrentPageIndex, string.Empty, string.Empty, ref RecordCount).Tables[0];
            Pager.RecordCount = RecordCount;
            this.gvList.DataSource = dt;
            this.gvList.DataBind();
        }

        private void Bind_DataEdit(Guid ID) {
            this.MultiViewList.ActiveViewIndex = 1;
            ModelGZBrand model = new BLLGZBrand().GetModel(ID);
            this.txtBrandTitle.Text = model.BrandName;
            this.txtBrandReadme.Text = model.BrandShortCutName;
            this.txtBrandNo.Text = model.BrandNo;
            this.txtOid.Text = model.Oid.ToString();
            this.ddlIsShow.SelectedIndex =
                this.ddlIsShow.Items.IndexOf(
                 this.ddlIsShow.Items.FindByValue(model.IsShow.Value ? "1" : "0")
                );
            this.hfID.Value = model.ID.ToString();
            this.hfProductFilePath.Value = model.PicturePath ?? "";
            this.btnSave.Text = "保存修改";
        }

        private void Bind_Delete(Guid id) {
            bllGZProduct.Delete(id);
            Bind_DataList();
        }
        #endregion



        protected void Pager_PageChanged(object sender, EventArgs e) {
            this.Bind_DataList();
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            string id = this.hfID.Value;
            string BrandName = this.txtBrandTitle.Text;
            string BrandShortCutName = this.txtBrandReadme.Text;
            string PicturePath = this.hfProductFilePath.Value;
            string BrandNo = this.txtBrandNo.Text;
            string Oid = this.txtOid.Text;
            int mOid = 0;


            if (!int.TryParse(Oid,out mOid))
            {
                mOid = 0;
            }

            bool IsShow = this.ddlIsShow.SelectedValue == "1" ? true : false;
            ModelGZBrand model = new ModelGZBrand();
            model.BrandName = BrandName;
            model.IsShow = IsShow;
            model.BrandShortCutName = BrandShortCutName;
            model.BrandNo = BrandNo;
            model.Oid = mOid; 

            #region 上传图片
            if (this.fuProductFilePath.PostedFile.ContentLength > 0)
            {
                try
                {
                    string fullFilePath = this.fuProductFilePath.FileName;
                    string fileName = Path.GetFileName(fullFilePath);//文件名
                    string FileDate = DateTime.Now.ToString("yyyy-MM");
                    string saveFileName = "UploadFiles/" + FileDate + "/" + fileName;
                    string savePath = "UploadFiles/" + FileDate + "/";
                    string UploadFilesPath = Server.MapPath("~/");
                    if (!Directory.Exists(UploadFilesPath + savePath))
                    {
                        Directory.CreateDirectory(UploadFilesPath + savePath);
                    }
                    UploadFilesPath += saveFileName;
                    this.fuProductFilePath.SaveAs(UploadFilesPath);
                    model.PicturePath = saveFileName;
                }
                catch (Exception ex)
                {
                    model.PicturePath = "";
                }
            }
            else
            {
                model.PicturePath = PicturePath;
            }
            #endregion

            if (id == "0") {

                DataTable dt = new BLLGZBrand().GetList(" and BrandNo = '"+model.BrandNo+"'").Tables[0];

                if (dt != null && dt.Rows.Count > 0)
                {
                    Response.Write("<script>alert('该品牌编号已经存在！');window.location.href='/Admin/GZProduct/Brand/Admin_Brand.aspx'</script>");
                    Response.End();
                }
                 
                model.ID = Guid.NewGuid(); 
                new BLLGZBrand().Add(model);
            }
            else {
                model.ID = new Guid(id);
                new BLLGZBrand().Update(model);
            }
            this.Bind_DataList();
        }
        

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e) {
            Guid id = new Guid(Easp.Str.SetString(e.CommandArgument));
            string eName = Easp.Str.SetString(e.CommandName).ToLower();
            switch (eName) {
                case "go":
                    Bind_DataEdit(id);
                    break;
                case "del":
                    Bind_Delete(id);
                    break;
            }
        }

        #endregion 结束
    }
}